//
//  EFSNavigationController.m
//  EFSMobile
//
//  Created by Chuan on 11/17/15.
//  Copyright © 2015 Elephants Financial Service. All rights reserved.
//

#import "EFSNavigationController.h"
#import "UINavigationBar+UnderLine.h"
#import "UIViewController+PopViewController.h"
@interface EFSNavigationController()

@property(nonatomic, retain)UIView *hiddenView;

@end

@implementation EFSNavigationController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationBar.barTintColor = kRGBCOLOR(0, 63, 148);
    // 设置navigationBar 一像素下划线的样式
//    [self.navigationBar ul_setUnderLineColor:[UIColor EFSRedColor]];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count >= 1) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    //    [self hideBackButtonTitleForController:viewController];
    // Mine相关页面使用自定义NavigationBar，这里不能强制显示
//    [self setNavigationBarHidden:NO];
    [super pushViewController:viewController animated:animated];
}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    @weakify(self);
    if([self.viewControllers count] < [navigationBar.items count]) {
        return YES;
    }
    
    BOOL shouldPop = YES;
    UIViewController* vc = [self topViewController];
    if([vc respondsToSelector:@selector(shouldPopViewController)]) {
        shouldPop = [vc shouldPopViewController];
    }
    
    if(shouldPop) {
        dispatch_async(dispatch_get_main_queue(), ^{
            @strongify(self);
            [self popViewControllerAnimated:YES];
        });
    } else {
        // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments /34452906
        for(UIView *subview in [navigationBar subviews]) {
            if(subview.alpha < 1.) {
                [UIView animateWithDuration:.25 animations:^{
                    subview.alpha = 1.;
                }];
            }
        }
    }
    
    return NO;
}

#pragma mark - Make NavigationBar Hidden

- (void)setNavigationBarCouldBeInvisible {
    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setShadowImage:[UIImage new]];
    [self.navigationBar setTranslucent:YES];
    
    CGRect frame = self.navigationBar.frame;
    self.hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height+20)];
    self.hiddenView.backgroundColor = self.navigationBar.barTintColor;
    [self.view insertSubview:self.hiddenView belowSubview:self.navigationBar];
}

- (void)setNavigationBarVisible:(BOOL) visible {
    if (visible) {
        [self.navigationBar setAlpha:1];
        [self setNavigationBarHidden:NO];
        return;
    }
    [UIView animateWithDuration:0.4 animations:^{
        [self.navigationBar setAlpha:0];
    }];
}

- (void)setNavigationBarAlpha:(float) ratio {
    [self.hiddenView setAlpha:ratio];
}

@end
